package com.zy.algorithm.controller.screen;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Rect;
import android.view.View;

import com.zy.algorithm.R;
import com.zy.algorithm.bean.InsertionSortStepBean;
import com.zy.algorithm.bean.SortStepBean;
import com.zy.algorithm.controller.IndexController;
import com.zy.algorithm.fragment.BaseAlgorithmFragment;
import com.zy.utils.AnimatorUtils;

import java.util.ArrayList;
import java.util.List;

//首个屏幕-动画控制器
public class InsertionFirstScreenController extends BaseBallFirstScreenController {
    private static final String TAG = "SelectSortBaseFragment";

    public InsertionFirstScreenController(View root, boolean colorAni, IndexController indexController) {
        super(root, colorAni, indexController);
    }

    @Override
    protected void setOpIndexStatues(SortStepBean bean) {
        //super.setOpIndexStatues(bean);
    }

    @Override
    protected void showPK(SortStepBean curStepBean, BaseAlgorithmFragment.StepListener listener) {
        //super.showPK(curStepBean, listener);
        if (!(curStepBean instanceof InsertionSortStepBean)) {
            listener.nextStep();
            return;
        }
        InsertionSortStepBean bean = (InsertionSortStepBean) curStepBean;

        //当前比较的数字View
        View originFirstTV = getDataViewByIndex(curStepBean.getOpFirstIndex());
        View originSecondTV = getDataViewByIndex(curStepBean.getOpSecondIndex());

        Rect originFirstTVRect = AnimatorUtils.getGlobalVisibleRect(originFirstTV);
        Rect originSecondTVRect = AnimatorUtils.getGlobalVisibleRect(originSecondTV);


        List<Animator> animatorList = new ArrayList<>();

        //出来
        // 1. 第一次出来 - 两个
        if (bean.isCompareFirstInPer()) {
            Animator move = AnimatorUtils.getMove(originSecondTV, pk_op);
            Animator hide = AnimatorUtils.getAlpha(originSecondTV, false);
            Animator show = AnimatorUtils.getAlpha(pk_op, true);


            show.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation, boolean isReverse) {
                    super.onAnimationStart(animation, isReverse);
                    if (checkActivityDestroyed()) {
                        return;
                    }
                    pk_op.setText("" + bean.getOpSecondV());
                    pk_op.setVisibility(View.VISIBLE);
                    pk_op.setBackgroundColor(resources.getColor(R.color.statues_sorting));
                }
            });


            Animator moveOut = AnimatorUtils.getTogetherStart(move, hide, show);

            animatorList.add(moveOut);
        }

        if (true) {
            if (bean.isResult()) {
                animatorList.add(AnimatorUtils.getShakeAnimator(originFirstTV));
            } else {
                animatorList.add(AnimatorUtils.getShakeAnimator(pk_op));
            }
        }

        if (bean.isMoveRight()) {
            View moveOrigin = getDataViewByIndex(bean.getMoveRightFirstIndex());
            View moveTarget = getDataViewByIndex(bean.getMoveRightSecondIndex());

            Animator moveRight = AnimatorUtils.getMove(moveOrigin, moveTarget);
            animatorList.add(moveRight);
        }

        if (bean.isMoveBack()) {
            View moveTarget = getDataViewByIndex(bean.getMoveBackIndex());
            Animator moveBack = AnimatorUtils.getMove(pk_op, moveTarget);
            moveBack.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation, boolean isReverse) {
                    super.onAnimationEnd(animation, isReverse);
                    if (checkActivityDestroyed()) {
                        return;
                    }
                    pk_op.setTranslationX(0);
                    pk_op.setTranslationY(0);
                    pk_op.setScaleX(1f);
                    pk_op.setScaleY(1f);
                    pk_op.setVisibility(View.VISIBLE);
                    pk_op.setText("");
                    pk_op.setBackground(resources.getDrawable(R.drawable.background_line));
                }
            });

            animatorList.add(indexController.getMinIndexAnimator(bean.getMoveBackIndex()));


            animatorList.add(moveBack);
        }

        // 2. 后续就是 - 一个出来，key值保持不动

        //比较

        //后移

        //如果找到位置-数据回设
        startStepAnimator(animatorList, listener);
    }
}
